package net.etfbl.projekat.verko.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Date;

import net.etfbl.projekat.verko.dto.Film;
import net.etfbl.projekat.verko.dto.Repertoar;

public class RepertoarDAO {

	/**
	 * 
	 */
	
	public static ArrayList<Repertoar> getAllRepertoar(DataManager dm)
	{
		int id_repertoar;
		String termini;
		Date prikazivanje;
		Date datum;
		boolean aktivan;
		int link_to_film;
        ArrayList<Repertoar> rezultat= new ArrayList<Repertoar>();
		final String upit = "select * from tbl_repertoar";
		
		Connection conn = dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps=conn.prepareStatement(upit);
				try
				{
					ResultSet rs=ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_repertoar=rs.getInt(1);
							termini=rs.getString(2);
							prikazivanje=rs.getDate(3);
							datum=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							link_to_film=rs.getInt(6);
							rezultat.add(new Repertoar(id_repertoar, termini, prikazivanje, datum, aktivan, link_to_film));
						}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch (SQLException ex)
			{
				System.out.println("Greska u RepertoarDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;	
		
	}
	
	public static void addRepertoar(DataManager dm, Repertoar rep)
	{
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			final String upit= "insert into tbl_repertoar values(null,?,?,curdate(),?,?)";
			try {
				PreparedStatement ps=conn.prepareStatement(upit);
				ps.setString(1, rep.getTermini());
				ps.setDate(2, (Date) rep.getPrikazivanje());
				//ps.setDate(3, (Date) rep.getDatum());
				ps.setBoolean(4, rep.isAktivan());
				ps.setInt(5, rep.getLink_to_film());
				
				try
				{
					ps.execute();
				}
				finally
				{
					ps.close();
				}
				
			} 
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				System.out.println("Greska u RepertoarDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
			
		}
	}
	
	public static void updateRepertoar(DataManager dm, Repertoar rep)
	{
		Connection conn= dm.checkOutConnection();
		
		if(conn!= null)
		{
			final String upit= "update tbl_repertoar set  termini=?, prikazivanje=?, datum=?, aktivan=?, tbl_film_id_film=? where id_repertoar = "+rep.getId_repertoar();
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				ps.setString(1, rep.getTermini());
				ps.setDate(2, (java.sql.Date) rep.getPrikazivanje());
				ps.setDate(3, (java.sql.Date) rep.getDatum());
				ps.setBoolean(4, rep.isAktivan());
				ps.setInt(5, rep.getLink_to_film());
				try
				{
					ps.execute();
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException ex)
			{
				System.out.println("Greska u RepertoarDAO: "+ex.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
	}
	
	public static ArrayList<Repertoar> filterByPrikazivanje(DataManager dm, Date prikaz )
	{
		int id_repertoar;
		String termini;
		Date prikazivanje;
		Date datum;
		boolean aktivan;
		int link_to_film;
		ArrayList<Repertoar> rezultat= new ArrayList<Repertoar>();
		Connection conn = dm.checkOutConnection();
		
		if(conn!=null)
		{
			final String upit= "select * from tbl_repertoar where prikazivanje="+((java.sql.Date)prikaz).toString();
			
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(!rs.getBoolean(5))
								continue;
							id_repertoar=rs.getInt(1);
							termini=rs.getString(2);
							prikazivanje=rs.getDate(3);
							datum=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							link_to_film=rs.getInt(6);
							rezultat.add(new Repertoar(id_repertoar, termini, prikazivanje, datum, aktivan, link_to_film));
						}
			
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u RepertoarDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Repertoar> filterByDatum(DataManager dm, Date datum1)
	{
		int id_repertoar;
		String termini;
		Date prikazivanje;
		Date datum;
		boolean aktivan;
		int link_to_film;
		ArrayList<Repertoar> rezultat= new ArrayList<Repertoar>();
		Connection conn = dm.checkOutConnection();
		
		if(conn!=null)
		{
			final String upit= "select * from tbl_repertoar where datum"+((java.sql.Date)datum1).toString();
			
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(!rs.getBoolean(5))
								continue;
							id_repertoar=rs.getInt(1);
							termini=rs.getString(2);
							prikazivanje=rs.getDate(3);
							datum=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							link_to_film=rs.getInt(6);
							rezultat.add(new Repertoar(id_repertoar, termini, prikazivanje, datum, aktivan, link_to_film));
						}
			
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u RepertoarDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Repertoar> filterByFilm(DataManager dm, Film film)
	{
		int temp=film.getId_film();
		int id_repertoar;
		String termini;
		Date prikazivanje;
		Date datum;
		boolean aktivan;
		int link_to_film;
		ArrayList<Repertoar> rezultat = new ArrayList<Repertoar>();
		final String upit="select * from tbl_repertoar where tbl_film_id_film="+temp;
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(!rs.getBoolean(5))
								continue;
							id_repertoar=rs.getInt(1);
							termini=rs.getString(2);
							prikazivanje=rs.getDate(3);
							datum=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							link_to_film=rs.getInt(6);
							rezultat.add(new Repertoar(id_repertoar, termini, prikazivanje, datum, aktivan, link_to_film));
						}
			
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u RepertoarDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		
		if(!film.isAktivan())
		{
			rezultat=null;
		}
		return rezultat;
	}
	
	public static ArrayList<Repertoar> filterByFilmId(DataManager dm, int filmId)
	{
		int id_repertoar;
		String termini;
		Date prikazivanje;
		Date datum;
		boolean aktivan;
		int link_to_film;
		ArrayList<Repertoar> rezultat = new ArrayList<Repertoar>();
		final String upit="select * from tbl_repertoar where tbl_film_id_film="+filmId;
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							if(!rs.getBoolean(5))
								continue;
							id_repertoar=rs.getInt(1);
							termini=rs.getString(2);
							prikazivanje=rs.getDate(3);
							datum=rs.getDate(4);
							aktivan=rs.getBoolean(5);
							link_to_film=rs.getInt(6);
							rezultat.add(new Repertoar(id_repertoar, termini, prikazivanje, datum, aktivan, link_to_film));
						}
			
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u RepertoarDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
}
